package com.haolp.sys.common.config;

import com.haolp.sys.service.realm.ShiroUserRealm;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cache.MemoryConstrainedCacheManager;
import org.apache.shiro.mgt.RememberMeManager;
import org.apache.shiro.session.mgt.DefaultSessionManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;

@Configuration
public class SpringShiroConfig { //相当于 spring-shiro.xml
    @Bean("securityManager")
    public SecurityManager securityManager(ShiroUserRealm realm,
                                           CacheManager cacheManager,
                                           RememberMeManager rememberMeManager,
                                           SessionManager sessionManager) {
        DefaultWebSecurityManager sManager = new DefaultWebSecurityManager();
        sManager.setRealm(realm);
        //设置缓存
        sManager.setCacheManager(cacheManager);
        //设置记住我功能
        sManager.setRememberMeManager(rememberMeManager);
        sManager.setSessionManager(sessionManager);
        return sManager;
    }

    /**
     * 基于此对象创建过滤器工厂，通过过滤器工厂创建过滤器，通过过滤器对请求进行过滤。
     *
     * @return
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean sfBean = new ShiroFilterFactoryBean();
        //设置安全管理器
        sfBean.setSecurityManager(securityManager);
        //设置登录请求页面的URL。
        sfBean.setLoginUrl("/doLoginUI");
        //定义map指定请求过滤规则(哪些资源允许匿名访问，哪些需要认证访问).
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        //静态资源允许匿名访问："anon".
        map.put("/bower_components/**", "anon");
        map.put("/build/**", "anon");
        map.put("/dist/**", "anon");
        map.put("/plugins/**", "anon");
        map.put("/user/doLogin", "anon");
        //
        map.put("/doLogout", "logout");
        //除了匿名访问的资源外，其余的都要通过认证("authc")，后访问。
        //map.put("/**", "authc");
        map.put("/**", "user");
        sfBean.setFilterChainDefinitionMap(map);
        return sfBean;
    }

    /**
     * shiro框架中的授权是基于spring中的AOP规范做了一个具体实现
     * AuthorizationAttributeSourceAdvisor中定义了切入点和通知
     * 切入点需要使用  @RequiresPermissions注解进行描述，代理对象检测
     * 到方法上有该注解就会调用通知对象进行功能增强
     *
     * @param securityManager
     * @return
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }
    @Bean
    public CacheManager shiroCacheManager(){
        return new MemoryConstrainedCacheManager();
    }
    @Bean
    public RememberMeManager rememberMeManager(){
        CookieRememberMeManager cManager=new CookieRememberMeManager();
        SimpleCookie cookie=new SimpleCookie("rememberMe");
        //设置cookie的过期实践内部，如下设置为7天，单位为秒
        cookie.setMaxAge(7*24*60*60);
        cManager.setCookie(cookie);

        return cManager;
    }
    @Bean
    public SessionManager sessionManager(){
        //DefaultSessionManager dsManager=new DefaultSessionManager();
        DefaultWebSessionManager dsManager=new DefaultWebSessionManager();
        //全局会话超时时间设置，默认时间是30分钟，单位是毫秒
        dsManager.setGlobalSessionTimeout(60*1000);
        //是否开启删除无效的session对象，默认是true
        dsManager.setDeleteInvalidSessions(true);
        //是否开启定时调度进行检测过期session，默认是true
        dsManager.setSessionValidationSchedulerEnabled(true);
        //取消url后面的JSESSIONID
        dsManager.setSessionIdCookieEnabled(false);
        return dsManager;
    }
}